package com.example.wastes.user.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.example.wastes.common.pojo.Admin;
import com.example.wastes.common.pojo.User;
import com.example.wastes.common.util.JwtUtils;
import com.example.wastes.common.util.R;
import com.example.wastes.user.mapper.UserMapper;
import com.example.wastes.user.service.UserService;
import com.fasterxml.jackson.databind.util.JSONPObject;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static jdk.nashorn.internal.runtime.PropertyDescriptor.GET;

@RestController
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private JwtUtils jwtUtils;
    @GetMapping("/getAllUser")
    @ApiOperation("查询用户所有信息")
    public R getAllUser(){
        return new R(true,userService.findall());
    }
    @PostMapping("/saveUser")
    @ApiOperation("添加用户信息")
    public R saveUser(@RequestBody User user){
        return new R(userService.save(user));
    }
    @PutMapping("/updateUser")
    @ApiOperation("修改用户信息")
    public R updateUser(@RequestBody User user){
        return new R(userService.updateById(user));
    }
    @DeleteMapping("/deleteUser")
    @ApiOperation("删除用户信息")
    public R deleteUser(Integer id){
        return new R(userService.removeById(id));
    }
    //模糊查询
    @GetMapping("/findUserName")
    @ApiOperation("通过名字查所有")
    public R findUserName(String username){
        return new R(true,userService.findUserName(username));
    }
    @GetMapping("/findByidAll")
    @ApiOperation("通过id查所有")
    public R findByidAll(Integer id){
        return new R(true,userService.findByidAll(id));
    }

    @PostMapping("/upload1")
    //上传头像
    public String upload1(User user, MultipartFile filename, HttpServletRequest request){
        int i = userService.upload1(user,filename,request);
        if (i>0){
            return "上传成功";
        }else{
            return "上传失败";
        }
    }

    @GetMapping("/login1")
    //用户登录
    public Object tologin1(@RequestParam(value = "username") String username,@RequestParam(value = "password") String password){
        long userId=0;
        Map<String, Object> map = new HashMap<>();
        if(username == null || password == null){
            map.put("code",100);
            map.put("msg","请求参数不能为空");
        }else {
            User user = userService.tologin1(username);
            if (user !=null){
                if (password.equals(user.getPassword())){
                    userId = 1;
                    String token= jwtUtils.generateToken(userId);
                    map.put("code",200);
                    map.put("msg","登陆成功");
                    map.put("token",token);
                    map.put("user",user);
                }else {
                    map.put("code",103);
                    map.put("msg","用户名和密码错误");
                }
            }else{
                map.put("code",102);
                map.put("msg","该用户不存在");
            }
        }
        return map;
    }


    @GetMapping("/register1")
    public String register1(String username,String password){
        if (StringUtils.isEmpty(username)){
            return "用户名不允许为空";
        }
        if (StringUtils.isEmpty(password)){
            return "密码不允许为空";
        }
        User user = userService.findName(username);
        if (user != null){
            return "注册失败，用户存在";
        }
        int resultCount = userService.saveUser(username, password);
        System.out.println(resultCount);
        if (resultCount == 0) {
            return "注册失败";
        }
        return "注册成功";
    }
    //用户提现
    @GetMapping("/updateMoney")
    public String updateMoney(Integer id){
        List<User> user1 = userService.findByidAll(id);
        int a = user1.get(0).getMoney();
        System.out.println(a);
        if (a == 0){
            return "提现失败";
        }else {
            userService.updateMoney(id);
            return "提现成功";
        }

    }
    //充值积分
    @GetMapping("/updateIntegral")
    public String updateIntegral(Integer id){
        List<User> user1 = userService.findByidAll(id);
        int b = user1.get(0).getMoney();
        int c = user1.get(0).getIntegral();
        int sum = b + c;
        if (b == 0){
            return "充值失败";
        }else {
            userService.updateMoney(id);
            User user = new User();
            user.setId(id);
            user.setIntegral(sum);
            userService.updateById(user);
            return "充值成功";
        }
    }
}